package com.exsun.ex1077.ui.activity.mileage

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.drake.brv.PageRefreshLayout
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.MileageRank
import com.exsun.ex1077.network.entity.Page
import com.zhangping.fastjetpack.model.Tree
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.model.KeyValue

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2023/3/27 9:50
 */
class MileageListViewModel: BaseViewModel() {

    val pageSize = 20
    val searchWord = StringObservableField()
    var startTime: String = ""
    var endTime: String = ""
    var deptIdList: List<String>? = null
    var manageTypeIdList: List<String>? = null
    val deptTreeResult = MutableLiveData<ResultState<List<Tree>>>()
    val dataLiveData = MutableLiveData<ResultState<Page<MileageRank>>>()
    val objectTypeResult = MutableLiveData<ResultState<List<KeyValue>>>()

    fun loadDeptTree(){
        request({ apiService.vehicleDeptTree()}, deptTreeResult)
    }

    fun loadData(pageIndex: Int = PageRefreshLayout.startIndex){
        request({ apiService.mileageCountList(JSONObject().apply {
            put("pageIndex", pageIndex)
            put("pageSize", pageSize)
            put("startTime", startTime)
            put("endTime", endTime)
            if (searchWord.get().isNotEmpty()){
                put("text", searchWord.get())
            }
            if (!deptIdList.isNullOrEmpty()){
                put("companyIds", deptIdList)
            }
            if (!manageTypeIdList.isNullOrEmpty()){
                put("objTypes", manageTypeIdList)
            }
        })}, dataLiveData)
    }

    fun loadManageTypeList(){
        request({ apiService.manageTypeList()}, objectTypeResult)
    }

}